package com.wisdomparking.control.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wisdomparking.bean.Manager;
import com.wisdomparking.bean.Parking;
import com.wisdomparking.bean.Rawdata;
import com.wisdomparking.config.result.TempResult;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.ManagerService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.RawdataService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.tools.MD5;
import com.wisdomparking.tools.PageForAjax;
import com.wisdomparking.tools.ToolValid;

/**
 * 车场管理
 */
@RestController
@RequestMapping("/web/parkingCenter")
public class ParkingManagerController extends BaseController{
	
	@Autowired ParkingService parkingService;
	@Autowired UserService userService;
	@Autowired ManagerService managerService;
	@Autowired RawdataService rawdataService;
	
	
	/**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
    	
        return new ModelAndView("parking/index");
    }
	
	/**
     * 左侧
     *
     * @return
     */
    @RequestMapping(value = "/left")
    public ModelAndView left(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("parking/left");
    }
    
    /**
     * 转到车场信息处理
     *
     * @return
     */
    @RequestMapping(value = "/handlePage")
    public ModelAndView handlePage() {
    	
    	if(ToolValid.isNotEmpty(request.getParameter("id"))){
    		Parking parking = parkingService.getParking(Integer.parseInt(request.getParameter("id")));
    		
    		request.setAttribute("parking", parking);
    	}
    	
        return new ModelAndView("parking/op");
    }
    
    
    /**
	 * 获取车场信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getParking")
	public ModelAndView getParking(Integer id){
		Parking parking = parkingService.getParking(id);
		
		request.setAttribute("parking", parking);
		
		return new ModelAndView("parking/op");
	}
	
	
	/**
	 * 删除车场
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delParking")
	public TempResult delOrders(Integer id){
		boolean flag = false;
		Parking parking = parkingService.getParking(id);
		parking.setDelFlag(true);
		flag = parkingService.updateParking(parking, false);
		
		return new TempResult(flag, "");
	}
	
	
	
	
	
	
	/**
	 * 获取车场信息列表
	 * @param id
	 * @return
	 */
	@RequestMapping("/getParkingList")
	public ModelAndView getParkingList(HttpServletRequest request){
		
		Manager loginManger = (Manager)request.getSession().getAttribute(Constants.LOGININFO);
		
		String url = request.getContextPath() + "/web/parkingCenter/getParkingList";
		String query = "";
		PageForAjax pagecontent = null;
		
		int pageNo = 1;
		int pageSize = 10;
		int totalcount = 0;
		
		if(null!=request.getParameter("pageNo")){
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		
		if(null!=request.getParameter("pageSize")){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		Map<String, Object> map = new HashMap<>();
		
		map.put("start", (pageNo-1) * pageSize);
		map.put("limit", pageSize);
		map.put("delFlag", 0);
		
		if(ToolValid.isNotNull(loginManger)){
			switch (loginManger.getManagerGrade()) {
			case 0:
				break;
			case 1:
				map.put("managerId", loginManger.getId());
				break;
			case 2:
				map.put("managerId", loginManger.getManagerId());
				break;

			default:
				break;
			}
		}
		
		if(ToolValid.isNotEmpty(request.getParameter("key"))){
			map.put("likename", request.getParameter("key"));
			request.setAttribute("key", request.getParameter("key"));
		}
		
		List<Parking> list = parkingService.getParkingList(map);
		totalcount = new Long(parkingService.getCountByParking(map)).intValue();

		pagecontent = new PageForAjax(pageNo, pageSize, totalcount, url, query);

		request.setAttribute("parkingList", list);
		request.setAttribute("nowpage", pagecontent);
		
		return new ModelAndView("parking/parkings");
	}
	
	
	/**
	 * 新增车场信息
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping("/handleManager")
	public TempResult handleManager(Parking parking){
		boolean flag = false;
		if(ToolValid.isNotNull(parking)){
			parking.setCreateTime(new Date());
			parking.setIdentification(MD5.getMD5Str(parking.getName() + parking.getCreateTime().getTime()));
			
			Manager loginManger = (Manager)request.getSession().getAttribute(Constants.LOGININFO);
			if(ToolValid.isNotNull(loginManger)){
				if(loginManger.getManagerGrade()==1){
					parking.setManagerId(loginManger.getId());
				}
				else{
					parking.setManagerId(loginManger.getManagerId());
				}
			}
			
			if(ToolValid.isNotEmpty(parking.getId()) && parking.getId()>0){
				flag = parkingService.updateParking(parking, false);
			}
			else{
				flag = parkingService.addParking(parking, true);
			}
			
			
			
			if(flag){
				Rawdata rawdata = new Rawdata();
				rawdata.setIdentification(parking.getIdentification());
				rawdata.setPutInStorageMark(true);
				rawdata.setUpdateTime(new Date());
				
				rawdataService.addRawdata(rawdata, true);
			}
			
		}
		return new TempResult(flag, "");
	}
	
}
